package leetcode.method.pointer;

public class Program11MaxArea {
    public Program11MaxArea() {
    }

    public int maxArea(int[] height) {
//        return person(height);
        return office(height);
    }

    public int office(int[] height) {
        int l = 0, r = height.length - 1;
        int ans = 0;
        while (l < r) {
            int area = Math.min(height[l], height[r]) * (r - l);
            ans = Math.max(ans, area);
            // 移动值小的位置
            if (height[l] <= height[r]) {
                ++l;
            }
            else {
                --r;
            }
        }
        return ans;
    }

    /**
     * 超时
     * @param height
     * @return
     */
    public int person(int[] height) {

        int max = 0;
        for (int i = 0; i < height.length - 1; i++) {
            for (int j = height.length - 1; j > i; j--) {
                max = Math.max(max, Math.min(height[i], height[j]) * (j - i));
            }
        }
        return max;
    }

    public static void main(String[] args) {
        Program11MaxArea obj = new Program11MaxArea();
        int[] height = {1,8,6,2,5,4,8,3,7};
        System.out.println(obj.maxArea(height));
        int[] height1 = {1,2,4,3};
        System.out.println(obj.maxArea(height1));
    }
}
